package com.qing.polymorphic;

/**
 * 类型转换
 * 1：把子类转为父类，向上转型
 * 2：把父类转为子类，向下转型，需要强转
 * 3：方便方法的调用，减少重复的代码
 */
public class InstanceofMain {
    public static void main(String[] args) {
        //instanceof（x instanceof y）判断x是不是属于y类型
        //Object > Person > Student
        //Object > String
        //Object > Person > Teacher
        Object o = new Student();
        System.out.println(o instanceof Student);//true
        System.out.println(o instanceof Person);//true
        System.out.println(o instanceof Object);//true
        System.out.println(o instanceof Teacher);//false
        System.out.println(o instanceof String);//false
        System.out.println("===========================================");
        Person p = new Student();
        System.out.println(p instanceof Student);//true
        System.out.println(p instanceof Person);//true
        System.out.println(p instanceof Object);//true
        System.out.println(p instanceof Teacher);//false
        // System.out.println(p instanceof String);//person和string没有半点关系，编译都不会通过
        System.out.println("===========================================");
        Student s = new Student();
        System.out.println(s instanceof Student);//true
        System.out.println(s instanceof Person);//true
        System.out.println(s instanceof Object);//true
        //System.out.println(s instanceof Teacher);//false 两个子类之间不可比较，编译报错
        System.out.println("===========================================");
        //类型转换
        Person pp = new Person();
        Student ss = new Student();
        pp = ss;//低转高无需转换
        ss = (Student) pp;//高转低需要强制类型转换,强制将父亲转成子类对象
        System.out.println("===========================================");
        Person person = new Student();
        //person.oneself();//父类无法直接调用子类中方法，需要将父类对象转成子类对象，高转低需要强转，向下转型
        Student student = (Student) person;//person强转成student
        student.oneself();
        ((Student) person).oneself();//简写
        System.out.println("===========================================");
        Person meng = new Student();//低转高
        Student meng1 = (Student) new Person();//高转低需要强转


    }
}
